package com.fz.controller;

import com.fz.common.Result;
import com.fz.controller.dto.LoginDTO;
import com.fz.controller.request.AdminPageRequest;
import com.fz.controller.request.LoginRequest;
import com.fz.controller.request.PasswordRequest;
import com.fz.controller.request.UserPageRequest;
import com.fz.pojo.Admin;
import com.fz.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin")
@CrossOrigin
public class AdminController {
    @Autowired
    private AdminService adminService;

    //查询用户
    @GetMapping("/list")
    public Result list() {
        List<Admin> admin = adminService.list();
        return Result.success(admin);
    }

    //通过id查询用户
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        Admin admin = adminService.getById(id);
        return Result.success(admin);
    }

    //分页
    @GetMapping("/page")
    public Result page(AdminPageRequest adminPageRequest) {
        return Result.success(adminService.page(adminPageRequest));
    }


    // 添加
    @PostMapping("/addAdmin")
    public Result addAdmin(@RequestBody Admin admin) {//这里要加@RequestBody 如果不加添加都是null
        adminService.addAdmin(admin);
        return Result.success();
    }

    //更新
    @PutMapping("/update")
    public Result update(@RequestBody Admin admin) {
        adminService.update(admin);
        return Result.success();
    }

    //删除
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        adminService.deleteById(id);
        return Result.success();
    }

    // 管理员登录
    @PostMapping("/login")
    public Result login(@RequestBody LoginRequest loginRequest) {//这里要加@@RequestBody 如果不加添加都是null
        LoginDTO login = adminService.login(loginRequest);
//        if (login==null){
//            return Result.error("用户名或密码错误");
//        }
        return Result.success(login);
    }
    //管理员修改密码
    @PutMapping("/updatePassword")
    public Result updatePassword(@RequestBody PasswordRequest passwordRequest) {
        adminService.updatePassword(passwordRequest);
        return Result.success();
    }


}
